package net.vistiyos.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

public class Hit implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	private String extension;
	private String size;
	private String name;
	private String description;
	private URL address;
	private URL details;
	private String[] tags;
	private float rate;
	private Date date;
	private String related;
	private String password;
	
	public Hit(Map<String, String> attributes) throws ParseException, MalformedURLException {
		this.id=Integer.parseInt(attributes.get("id"));
		this.extension=attributes.get("extension");
		this.size=attributes.get("size");
		this.name=attributes.get("name");
		this.description=attributes.get("description");
		this.address=new URL(attributes.get("address"));
		this.details=new URL(attributes.get("details"));
		this.tags=attributes.get("tags").split("|");
		this.rate=Float.parseFloat(attributes.get("rate"));
		SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		this.date=formatoDelTexto.parse(attributes.get("added"));
		this.related=attributes.get("related");
		this.password=attributes.get("password");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Hit [id=" + id + ", extension=" + extension + ", size=" + size
				+ ", description=" + description + ", address=" + address
				+ ", details=" + details + ", tags=" + Arrays.toString(tags)
				+ ", rate=" + rate + ", date=" + date + ", related=" + related
				+ ", password=" + password + "]";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getExtension() {
		return extension;
	}

	public void setExtension(String extension) {
		this.extension = extension;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public URL getAddress() {
		return address;
	}

	public void setAddress(URL address) {
		this.address = address;
	}

	public URL getDetails() {
		return details;
	}

	public void setDetails(URL details) {
		this.details = details;
	}

	public String[] getTags() {
		return tags;
	}

	public void setTags(String[] tags) {
		this.tags = tags;
	}

	public float getRate() {
		return rate;
	}

	public void setRate(float rate) {
		this.rate = rate;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getRelated() {
		return related;
	}

	public void setRelated(String related) {
		this.related = related;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public byte[] serializar () throws IOException {
		ByteArrayOutputStream bs= new ByteArrayOutputStream();
		ObjectOutputStream os = new ObjectOutputStream (bs);
		os.writeObject(this);  // this es de tipo DatoUdp
		os.close();
		return bs.toByteArray(); // devuelve byte[]
    }
    
    static public Hit deserializar (byte[] bytes) throws IOException, ClassNotFoundException {
    	ByteArrayInputStream bs= new ByteArrayInputStream(bytes); // bytes es el byte[]
    	ObjectInputStream is = new ObjectInputStream(bs);
    	Hit objeto = (Hit)is.readObject();
    	is.close();
    	return objeto;
    }

}
